<?php

class Damages_Methods_Data_CarData {

  /**
   *
   * @var int 
   */
  public $id;

  /**
   *
   * @var string 
   */
  public $vin;

  /**
   *
   * @var string 
   */
  public $model;

  /**
   *
   * @var int 
   */
  public $comission;

  /**
   *
   * @var Damages_Methods_Data_DealerData 
   */
  public $dealer;

  /**
   *
   * @var Damages_Methods_Data_Brand 
   */
  public $brand;

  /**
   *
   * @var int[] 
   */
  public $carDamagesIds = array();

  public function __construct(DamagesCarData $data) {
    $this->id = $data->getId();
    $this->vin = $data->getVin();
    $this->model = $data->getName();
    $this->comission = $data->getComissionNo();
    if ($data->getDealersRelatedByDealerId())
      $this->dealer = new Damages_Methods_Data_DealerData($data->getDealersRelatedByDealerId());
    $this->brand = new Damages_Methods_Data_Brand($data->getDictBrands());
    $registeredDamages = $this->getRegisteredCarDamages($this->id);
    if (!$registeredDamages->isEmpty()){
      $this->setCarDamages($registeredDamages);
    }
  }

  public function setCarDamages($carDamages) {
    foreach ($carDamages as $carDamage) {
      $this->carDamagesIds[] = $carDamage->getId();
    }
  }

  /**
   * 
   * @param int $carId
   * @return type
   */
  private function getRegisteredCarDamages($carId) {
    $q = DamagesCarDamagesQuery::create()->joinProcessTypes()->filterByCarDataId($carId);
    if (Damages_Auth::getInstance()->getUser()->getPermissions()->getCode() == Damages_Auth::PERMISSION_SD) {
      $q->where('ProcessTypes.Code <> ?', DamagesProcessTypes::CODE_WAREHOUSE);
    }
    if (Damages_Auth::getInstance()->getUser()->getPermissions()->getCode() == Damages_Auth::PERMISSION_LS) {
      $q->where('ProcessTypes.Code = ?', DamagesProcessTypes::CODE_WAREHOUSE);
    }
    
    return $q->find();
  }

}